package com.xqboss.apps.service.nft;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.nft.NftCategory;
import com.xqboss.apps.mapper.nft.NftCategoryMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Spu品类表 服务实现类
 * </p>
 *
 * @author HaoLun
 * @since 2022-06-07
 */
@Service
public class NftCategoryService extends ServiceImpl<NftCategoryMapper, NftCategory> {


    public List<List<NftCategory>> getCategoryList(Long nftId) {
        QueryWrapper<NftCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(NftCategory::getNftId, nftId);
        List<NftCategory> nftCategoryList = this.list(queryWrapper);

        Map<Integer, List<NftCategory>> maps = new HashMap<>();
        nftCategoryList.forEach(nftCategory -> {
            Integer groupId = nftCategory.getGroupId();

            List<NftCategory> nftCategories;
            if (!maps.containsKey(groupId)) {
                nftCategories = new ArrayList<>();
                nftCategories.add(nftCategory);
            } else {
                nftCategories = maps.get(groupId);
                nftCategories.add(nftCategory);
            }
            maps.put(groupId, nftCategories);

        });
        List<List<NftCategory>> lists = new ArrayList<>();
        for (Map.Entry<Integer, List<NftCategory>> entry : maps.entrySet()) {
            lists.add(entry.getValue());
        }
        return lists;
    }
}
